﻿using Mapster;
using Middleware.ApiTest.Contexts;
using Middleware.ApiTest.Models;
using Middleware.ApiTest.Services.Dtos;
using Microsoft.EntityFrameworkCore;
using Middleware.ApiTest.Attributes;

namespace Middleware.ApiTest.Services
{
    public class OrderServices: IOrderServices
    {
        private readonly MyDbContext _myDbContext;
        public OrderServices(MyDbContext myDbContext)
        {
            _myDbContext = myDbContext; 
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <returns></returns>
        [DataMasking]
        public async Task<List<OrderInfoDto>> GetOrderListAsync()
        {
            var list = await _myDbContext.OrderInfo
                          .Include(o => o.OrderPrice)       // 包含 OrderPrice 导航属性
                          .Include(o => o.OrderItems)       // 包含 OrderItems 集合
                          //.Include(o => o.ConsigneeInfo)    // 包含 ConsigneeInfo 导航属性
                          .ToListAsync();

            var dtoList = list.Adapt<List<OrderInfoDto>>();
            await Task.CompletedTask;
            return dtoList; 
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="createOrderDto"></param>
        /// <returns></returns>
        public async Task<bool> CreateOrderAsync(CreateOrderDto createOrderDto)
        {
            var model = createOrderDto.Adapt<OrderInfo>();
            _myDbContext.OrderInfo.Add(model);
            var result = await _myDbContext.SaveChangesAsync();
            return result > 0;
        }

    }
}
